<template>
    <a-form ref="formRef" :model="formData">
        <a-form-item field="type" label="藏品" label-col-flex="auto" :label-col-style="{ width: '85px' }">
            <a-select placeholder="请选择藏品" v-model="formData.product_id">
                <a-option v-for="item in select_list" :value="item.id">{{ item.name }}</a-option>
            </a-select>
        </a-form-item>
        <a-form-item field="type" label="类型" label-col-flex="auto" :label-col-style="{ width: '85px' }">
            <a-radio-group v-model="formData.type">
                <a-radio :value="1">公开购</a-radio>
                <a-radio :value="2">优先购</a-radio>
                <a-radio :value="3">权益购</a-radio>
            </a-radio-group>
        </a-form-item>
        <a-form-item field="is_show" label="是否显示" label-col-flex="auto" :label-col-style="{ width: '85px' }">
            <a-radio-group v-model="formData.is_show">
                <a-radio :value="0">是</a-radio>
                <a-radio :value="1">否</a-radio>
            </a-radio-group>
        </a-form-item>

        <a-form-item field="quota" label="限购" label-col-flex="auto" :label-col-style="{ width: '85px' }">
            <a-input-number v-model="formData.quota" placeholder="请输入限购" />
        </a-form-item>
        <a-form-item field="stock" label="增加库存" label-col-flex="auto" :label-col-style="{ width: '85px' }">
            <a-input-number v-model="formData.stock" placeholder="请输入增加库存" />
        </a-form-item>

        <a-form-item field="stock" label="日期" label-col-flex="auto" :label-col-style="{ width: '85px' }">
            <a-range-picker showTime v-model="default_value" @change="change" />
        </a-form-item>
    </a-form>
</template>
<script  setup>
import { ref } from "vue";
import request from '@/api/purchase'
import { Message } from "@arco-design/web-vue";
const emit = defineEmits(["refreshList"]);

const props = defineProps({
    id: [String, Number]
});

const formData = ref({});

let default_value = ref([]);
const getDeta = async () => {
    let { data: { data } } = await request.get_deta(props.id);
    let date = [data.start_date, data.end_date];
    default_value.value = date;

    data.start = data.start_date;
    data.end = data.end_date;
    formData.value = data;
};

const select_list = ref([]);
const get_select = async () => {
    let { data: { product: list } } = await request.get_select();
    select_list.value = list;
    props.id ? getDeta() : "";
};
get_select();

const change = (time) => {
    formData.value.start = time[0];
    formData.value.end = time[1];
};
const submit = async () => {
    await request.save(formData.value);
    Message.success("保存成功");
    setTimeout(() => emit("refreshList"), 500);
};
defineExpose({ submit });
</script>